package com.nj.dms.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nj.dms.bean.PaperMoldDocument;
import com.nj.dms.dao.PaperMoldDocumentDao;
import com.nj.dms.dao.impl.PaperMoldDocumentDaoImpl;

/**
 * 发布纸版文档
 */
@WebServlet("/AddPaperMoldDocument")
public class AddPaperMoldDocument extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		// 获取用户表单数据
		String title = request.getParameter("title");
		String rollNum = request.getParameter("rollNum");
		String location = request.getParameter("location");
		int thick = new Integer(request.getParameter("thick"));
		String description = request.getParameter("description");

		// 获取用户名
		String userName = (String) request.getSession().getAttribute("userName");

		// 设置审核状态

		int audit = thick == 1 ? 1 : 0;

		PaperMoldDocument pmd = new PaperMoldDocument(userName, title, description, thick, audit, rollNum, new Date(),
				new Date(), location);

		// 将数据传入Dao层

		PaperMoldDocumentDao pmddi = new PaperMoldDocumentDaoImpl();

		if (pmddi.addPaperMoldDocument(pmd)) {

			// 纸版文档增加成功，跳转到发布菜单

			response.sendRedirect("document/add-document-menu.jsp");

		} else {

			// 纸版文档增加失败，跳转到发布表单

			// 保存页面数据
			request.setAttribute("paperMoldDocument", pmd);

			request.getRequestDispatcher("document/add-paper-mold-document.jsp").forward(request, response);

		}

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
